layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var Advert = {
        tableId: "advertTable",    //表格id
        condition: {
        	nickname: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Advert.initColumn = function () {
        return [[
        	{type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'url',width: 200,align:'center', templet:'#imgTpl', title: '图片'},
        	{field: 'fileName', sort: true, title: '文件名'},
        	{field: 'fileSize', sort: true, title: '文件大小'},
        	{field: 'describeStr', sort: true, title: '文件描述'},
            {field: 'fileType', sort: true, title: '文件类型'},
            {field: 'createUser', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 300}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Advert.search = function () {
        var queryData = {};
        queryData['nickname'] = $("#nickname").val();
        table.reload(Advert.tableId, {where: queryData, page:{curr: 1}});
    };
    
    /**
     * 弹出添加
     */
    Advert.openAddAdvert = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加广告',
            area: ['800px', '800px'],
            content: Feng.ctxPath + '/advert/advert_add',
            end: function () {
            	Advert.search();
            }
        });
    };
    
    /**
     * 导出excel按钮
     */
    Advert.exportExcel = function () {
        var checkRows = table.checkStatus(Advert.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Advert.tableId,
        url: Feng.ctxPath + '/advert/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Advert.initColumn()
    });
    
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Advert.openDelAdvert = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/advert/del", function () {
                table.reload(Advert.tableId);
                Feng.success("删除成功!");
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("advertId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除'" + data.fileName + "' ?", operation);
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Advert.search();
    }); 
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
    	Advert.openAddAdvert();
    });
    // 导出excel
    $('#btnExp').click(function () {
    	Advert.exportExcel();
    });
    // 工具条点击事件
    table.on('tool(' + Advert.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'del') {
        	Advert.openDelAdvert(data);
        }else if(layEvent === 'subAdvertList'){
        	Advert.openSubAdvertList(data);
        }
    });
});
